/**
 *
 */package com.gcsj.gcsjspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gcsj.gcsjspringboot.common.annotation.SysLog;
import com.gcsj.gcsjspringboot.common.utils.DateUtils;
import com.gcsj.gcsjspringboot.common.utils.PageUtils;
import com.gcsj.gcsjspringboot.common.utils.R;
import com.gcsj.gcsjspringboot.common.validator.ValidatorUtils;
import com.gcsj.gcsjspringboot.constant.WorkStatusEnum;
import com.gcsj.gcsjspringboot.entity.WorkRecordEntity;
import com.gcsj.gcsjspringboot.service.WorkRecordService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * 考勤管理
 *
 * @author
 */
@RestController
@RequestMapping("/work")
public class WorkRecordController extends AbstractController {
    @Autowired
    private WorkRecordService workRecordService;
    String upTimeStr;
    String downTimeStr;
    String overTimeStr;
//    @Value("${work-up-time}")
//    String upTimeStr;
//
//    @Value("${work-down-time}")
//    String downTimeStr;
//    @Value("${work-over-time}")
//    String overTimeStr;

    /**
     * 所有考勤列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        Long userId = getUserId();
        params.put("userId", userId);
        PageUtils page = workRecordService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 考勤信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        WorkRecordEntity work = workRecordService.getById(id);
        return R.ok().put("work", work);
    }

//    @RequestMapping("/findby")
//    @ResponseBody
//    public R info(String username, String workdate) {
//        WorkRecordEntity work1 = new WorkRecordEntity();
//        work1.setUserName(username);
//        work1.setUserName(workdate);
//
//        ArrayList<WorkRecordEntity> list = workRecordService.findBy(work1);
//        return R.ok().put("data", list);
//    }

    /**
     * 保存考勤
     */
    @RequestMapping("/save")
    @RequiresRoles("admin")
    public R save(@RequestBody WorkRecordEntity work) {
        work.setCreateTime(DateUtils.currentTimeFormat());
        work.setUpdateTime(DateUtils.currentTimeFormat());
        workRecordService.save(work);
        return R.ok();
    }

    /**
     * 修改考勤
     */
    @RequestMapping("/update")
    @RequiresRoles("admin")
    public R update(@RequestBody WorkRecordEntity work) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        work.setUpdateTime(formatter.format(date));
        workRecordService.updateById(work);
        return R.ok();
    }

    /**
     * 删除考勤
     */
    @RequestMapping("/delete")
    @RequiresRoles("admin")
    public R delete(Integer id) {
        boolean b = workRecordService.removeById(id);
        if(b){
            return R.ok();
        }

        return R.error();
    }

}
